package com.zenithmind.chat.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 消息阅读记录VO - 遵循单一职责原则
 * 只负责消息阅读记录信息的视图展示
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "消息阅读记录VO")
public class ChatMessageReadVO {

    @Schema(description = "阅读记录ID")
    private String id;

    @Schema(description = "消息ID")
    private String messageId;

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "群组ID")
    private String groupId;

    @Schema(description = "阅读时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime readTime;

    @Schema(description = "阅读状态")
    private Integer readStatus;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "用户头像")
    private String userAvatar;

    @Schema(description = "用户真实姓名")
    private String realName;

    @Schema(description = "消息内容")
    private String messageContent;

    @Schema(description = "消息类型")
    private Integer messageType;

    @Schema(description = "发送者ID")
    private String senderId;

    @Schema(description = "发送者名称")
    private String senderName;

    /**
     * 获取显示的用户名
     * 
     * @return 显示用户名
     */
    public String getDisplayUsername() {
        if (realName != null && !realName.trim().isEmpty()) {
            return realName;
        }
        return username;
    }

    /**
     * 获取显示的用户头像
     * 
     * @return 显示用户头像
     */
    public String getDisplayUserAvatar() {
        if (userAvatar != null && !userAvatar.trim().isEmpty()) {
            return userAvatar;
        }
        return "/images/default-user-avatar.png";
    }

    /**
     * 检查是否已阅读
     * 
     * @return 是否已阅读
     */
    public boolean isRead() {
        return readStatus != null && readStatus == 1;
    }

    /**
     * 获取阅读状态名称
     * 
     * @return 阅读状态名称
     */
    public String getReadStatusName() {
        if (readStatus == null) {
            return "未知";
        }
        switch (readStatus) {
            case 0:
                return "未读";
            case 1:
                return "已读";
            default:
                return "未知";
        }
    }

    /**
     * 获取消息类型名称
     * 
     * @return 消息类型名称
     */
    public String getMessageTypeName() {
        if (messageType == null) {
            return "未知";
        }
        switch (messageType) {
            case 1:
                return "文本";
            case 2:
                return "图片";
            case 3:
                return "语音";
            case 4:
                return "视频";
            case 5:
                return "文件";
            case 6:
                return "位置";
            case 7:
                return "表情";
            case 8:
                return "链接";
            case 9:
                return "系统消息";
            default:
                return "未知";
        }
    }

    /**
     * 获取阅读时间描述
     * 
     * @return 阅读时间描述
     */
    public String getReadTimeDescription() {
        if (readTime == null) {
            return "未读";
        }
        
        LocalDateTime now = LocalDateTime.now();
        long minutes = java.time.Duration.between(readTime, now).toMinutes();
        
        if (minutes < 1) {
            return "刚刚";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        } else if (minutes < 1440) { // 24小时
            return (minutes / 60) + "小时前";
        } else {
            return (minutes / 1440) + "天前";
        }
    }

    /**
     * 获取消息内容摘要
     * 
     * @return 消息内容摘要
     */
    public String getMessageSummary() {
        if (messageContent == null || messageContent.trim().isEmpty()) {
            return "[" + getMessageTypeName() + "]";
        }
        
        if (messageContent.length() > 50) {
            return messageContent.substring(0, 50) + "...";
        }
        
        return messageContent;
    }

    /**
     * 检查是否为系统消息
     * 
     * @return 是否为系统消息
     */
    public boolean isSystemMessage() {
        return messageType != null && messageType == 9;
    }

    /**
     * 检查是否为媒体消息
     * 
     * @return 是否为媒体消息
     */
    public boolean isMediaMessage() {
        return messageType != null && (messageType >= 2 && messageType <= 5);
    }

    /**
     * 获取阅读延迟（分钟）
     * 
     * @return 阅读延迟
     */
    public Long getReadDelayMinutes() {
        if (readTime == null || createTime == null) {
            return null;
        }
        
        return java.time.Duration.between(createTime, readTime).toMinutes();
    }

    /**
     * 检查是否为快速阅读（5分钟内）
     * 
     * @return 是否为快速阅读
     */
    public boolean isQuickRead() {
        Long delay = getReadDelayMinutes();
        return delay != null && delay <= 5;
    }
}
